package other.xsy.passlys.receivable;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEvent;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventRequest;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventResponse;
import com.rkhd.platform.sdk.data.model.DebookingDetailed__c;
import com.rkhd.platform.sdk.data.model.Debooking__c;
import com.rkhd.platform.sdk.data.model.InvoiceDetail__c;
import com.rkhd.platform.sdk.data.model.Invoice__c;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.service.XoqlService;
import org.apache.commons.lang.StringUtils;
import other.xsy.passlys.util.HCXObjectService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description deBooking明细：新增、编辑回写主表booking数据
 * @Author 黎炎松
 * 2024-08-15 17:33
 */
public class DeBookingDetailRuleevent implements RuleEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "deBooking明细：新增、编辑回写主表booking数据 ==> ";

    public static void main(String[] args) {
        DeBookingDetailRuleevent deBookingDetailRuleevent = new DeBookingDetailRuleevent();
        RuleEventRequest request = new RuleEventRequest();
        request.setDataId(3411888499907227L);
        deBookingDetailRuleevent.execute(request);
    }

    /**
     * 触发规则触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public RuleEventResponse execute(RuleEventRequest request) {
        logger.info(LOGGER_MSG + "触发开始");
        Long dataId = request.getDataId();
        try {
            DebookingDetailed__c debookingDetailed = new DebookingDetailed__c();
            debookingDetailed.setId(dataId);
            debookingDetailed = hcxObjectService.get(debookingDetailed);

            Long debookingId = debookingDetailed.getDebooking__c();
            String sql = "select id,booking__c from debookingDetailed__c where debooking__c =" + debookingId;
            List<JSONObject> debookingDetailList = hcxObjectService.batchXoqlQuery(sql);
            String bookingIds = debookingDetailList.stream().map(v -> v.getString("booking__c")).distinct().collect(Collectors.joining(","));

            // 更新deBooking
            Debooking__c debooking = new Debooking__c();
            debooking.setId(debookingId);
            debooking.setBookingStr__c(bookingIds);
            Boolean update = hcxObjectService.update(debooking);
            logger.info(LOGGER_MSG + "update: " + update);

            logger.info(LOGGER_MSG + "执行结束");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        RuleEventResponse response = new RuleEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }


}
